


package com.es.business.contact;

import org.apache.log4j.Logger;

import com.es.dao.BusinessContactDAO;
import com.es.dao.ClientDAO;
import com.es.dao.ContactDAO;
import com.es.dao.factory.DAOFactory;
import com.es.exception.business.ESException;
import com.es.exception.dao.ESDataAccessException;
import com.es.model.BusinessContact;
import com.es.model.Client;
import com.es.model.Contact;
import com.es.model.Lead;
import com.es.util.ProjectConstant;


public class ContactImpl implements IContactBL {

	private static final Logger log = Logger.getLogger(ContactImpl.class);

	ContactDAO contactDAO = (ContactDAO) DAOFactory.getDataAccessObject(DAOFactory.CONTACT_DAO);
	ClientDAO clientDAO = (ClientDAO) DAOFactory.getDataAccessObject(DAOFactory.CLIENT_DAO);
	BusinessContactDAO businessContactDAO = (BusinessContactDAO) DAOFactory.getDataAccessObject(DAOFactory.BUSINESS_CONTACT_DAO);





	public Boolean createContact(Contact contact, Lead lead) throws ESException {


		Boolean check2;
		Boolean check3;

		if(contact== null){

			log.info("contact is null");

		}else if (lead == null) {

			log.info("lead is null");
		}

		log.info(lead.getFirstname());


		contact.setQualifiedId(lead.getLeadId());

		log.info(" contact.setQualifiedId(lead.getLeadId()) :: " + contact.getQualifiedId());
		contact.setLeadsource(lead.getLeadsource());
		contact.setCompaignsource(lead.getCompaignsource());

		try{

			contactDAO.saveContact(contact);
		}catch (ESDataAccessException e) {

			throw new ESException("contact could not be saved");
		}


		if(contact.getType().equals(ProjectConstant.CLIENT_CONTACT_CD)){

			log.info(" client " + ProjectConstant.CLIENT_CONTACT_CD);

			Client client = new Client();
			client.setContactId(contact.getContactId());
			//	check2 = contactImpl.saveClient(client);
			check2 = saveClient(client);

		}else if(contact.getType().equals(ProjectConstant.BUSINESS_CONTACT_CD)){

			log.info(" business contact " + ProjectConstant.BUSINESS_CONTACT_CD);

			BusinessContact businessContact = new BusinessContact();
			businessContact.setContactId(contact.getContactId());
			//	check3 = contactImpl.saveBusinessContact(businessContact);
			check3 = saveBusinessContact(businessContact);

		}
		return true;




	}


	/* 
	 * @author Bushra
	 * @param Client
	 * @return Boolean
	 * @see com.es.business.contact.IContactBL#saveClient(com.es.model.Client)
	 */
	public Boolean saveClient(Client client) throws ESException {

		try{

			clientDAO.saveClient(client);
			return true;

		}catch (ESDataAccessException e) {

			throw new ESException("client could not be created");
		}

	}



	/* 
	 * @author Bushra
	 * @param BusinessContact
	 * @return Boolean
	 * @see com.es.business.contact.IContactBL#saveBusinessContact(com.es.model.BusinessContact)
	 */
	public Boolean saveBusinessContact(BusinessContact businessContact)
	throws ESException {

		try{

			businessContactDAO.saveBusinessContact(businessContact);
			return true;

		}catch (ESDataAccessException e) {

			throw new ESException("business contact could not be created");
		}


	}



}
